<template>
	<view class="uni-login">
		<view class="personcss">
			<view class="p1">城市精细化管理</view>
			<view class="p2">业务处置系统</view>
			<view class="p3">User login</view>
		</view>
		<view class="uni-form">
			<view class="uni-form-item uni-column">
				<input type="text" name="userName" class="uni-input" v-model="loginForm.userName" placeholder="请输入用户名" />
				<image v-if="loginForm.userName" src='@/static/login/clearInput.png' class="icon_close" @click="clearUserName"></image>
			</view>
			<view class="uni-form-item uni-column">
				<input type="text" password="true" name="password" class="uni-input input-password" v-model="loginForm.password" placeholder="请输入密码"/>
				<image v-if="loginForm.password" src='@/static/login/clearInput.png' class="icon_close" @click="clearPassword"></image>
			</view>
			<view class="uni-btn-v">
				<button type="primary" @click="onSubmit()">登 录</button>
			</view>
		</view>

		<image src="@/static/login/qujianglogo.png" class="qjLogo"></image>
	</view>
</template>

<script>
	import Cookies from 'weapp-cookie'
	import URL from '../util/url.js'
	export default {
		data() {
			return {
				loading: false,
				loginForm: {
					userName: '',//机构处置人员：贾正西18049088808，崔丹丹18691610954，巡查人员：18629068569，标段机构接线员薛家祥：15319710129 ，平台监理：；13572800984  部门领导：15991695037
					password: '',
				},
				url:URL.LoginIn
			}
		},
		onLoad(){},
		mounted() {
			const userName = uni.getStorageSync('userName');
			const password = uni.getStorageSync('password');

			if (userName && password) {
				this.loginForm.userName = userName;
				this.loginForm.password = password;
			} else {
				this.loginForm.userName = '';
				this.loginForm.password = '';
			}
		},
		methods: {
			onSubmit(loginForm){
				if(!this.loginForm.userName || this.loginForm.userName.length==0){
					this.$tip.toast('请填写用户名');
					return;
				}
				if(!this.loginForm.password || this.loginForm.password.length==0){
					this.$tip.toast('请填写密码');
					return;
				}

				uni.showLoading({
					title: "正在登录",
				    mask: true,
				});

				this.loginForm.pushId = this.$store.getters['push/getPushId'];

				uni.request({
					url: this.url,
					method: 'POST',
					data: this.loginForm,
					success: (res) => {
						if(res.data.code == 200){
							//使用uni的storage存入token
							uni.setStorageSync('token', res.data.data.params.token);
							uni.setStorageSync('userInfo', res.data.data);

							uni.setStorageSync('userName', this.loginForm.userName);
							uni.setStorageSync('password', this.loginForm.password);

							this.$store.dispatch('setUserInfo', res.data.data);

							uni.reLaunch({
							    url: '/pages/home'
							});
						}else{
							this.$tip.toast(res.data.msg);
						}
						uni.hideLoading();
					},
					fail: (error) => {
						uni.hideLoading();
					}
				})
			},
			//清除用户名
			clearUserName(){
				this.loginForm.userName = '';
			},
			//清除密码
			clearPassword(){
				this.loginForm.password = '';
			}
		}
	}
</script>

<style lang="scss">
	uni-page-body{
		display: flex;
		flex-direction: column;
	}

	.uni-login{
		height: 100%;
		background: #fff;
		flex: 1;
		position: relative;
		.uni-form{
			width: 600rpx;
			height: 417rpx;
			background: white;
			border-radius: 40rpx;
			padding:40rpx;
			box-shadow: #b5cede 0px 0px 10px;
			margin: -80rpx auto 0;
			.uni-form-item{
				display: flex;
				flex-direction: row;
				align-items: center;
				border-bottom:1px solid #ccc;
				margin-bottom: 40rpx;
				padding: 0;
				padding-left:10rpx;
				.uni-input{
					height: 62rpx;
					font-size: 36rpx;
					padding-left: 0;
				}
				.input-password{
					// #ifdef APP-PLUS
					//letter-spacing: -5px;
					// #endif
				}
				image{
					width: 30rpx;
					height: 30rpx;
					margin-right:10rpx;
				}
			}
		}
		.personcss {
			background: linear-gradient(to right,#c4534c,#92231c);
			padding:60rpx 76rpx;
			color:#FFFFFF;
			height: 390rpx;
			.p1{
			  font-size: 44rpx;
			   font-weight: bolder;
			   margin-bottom: -20rpx;
			}
			.p2{
			  font-size: 66rpx;
			   font-weight: bolder;
			    margin-bottom: -20rpx;
			}
			.p3{
			  font-size: 36rpx;
			  font-weight: bolder;
			}
		}
		.qjLogo{
			width:150rpx;
			height:110rpx;
			position:absolute;
			right:80rpx;
			bottom:100rpx;
		}
		.uni-btn-v {
			margin-top: 48rpx;
			uni-button{
				line-height: 76rpx;
				font-size: 36rpx;
				border-radius: 100rpx;
				background: linear-gradient(to right,#c4534c,#92231c);
			}
		}
	}
</style>
